package com.xingkeduo.business.user.service;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.xingkeduo.dao.database.DatabaseAccessObject;
import com.xingkeduo.pojo.query.CraftsmanJobDetailQO;
import com.xingkeduo.pojo.table.CraftsmanJobDetailTO;
import com.xingkeduo.util.pojo.exception.DataException;
import com.xingkeduo.util.tool.CommonUtil;
import com.xingkeduo.util.tool.IdentityUtil;
import com.xingkeduo.util.tool.IdentityUtil.Worker;

/**
 * 一线员工工作属性类
 * 
 * @author 高杰
 * @time 2018年12月27日 上午11:45:44
 * @description
 */
@Component
public class CraftsmanJobDetailService {
	private static final String NAMESPACE = CommonUtil.formatNamespace(CraftsmanJobDetailTO.class);
	@Resource(name = "mysqlUserMasterDao")
	private DatabaseAccessObject<CraftsmanJobDetailTO, CraftsmanJobDetailQO> dao;

	/**
	 * 查询
	 * 
	 * @author 高杰
	 * @time 2018年12月27日 上午11:45:57
	 * @param code
	 * @return
	 * @throws Throwable
	 */
	public CraftsmanJobDetailTO selectByPrimary(long id) throws Throwable {
		return dao.select(NAMESPACE, id);
	}

	/**
	 * 查询
	 * 
	 * @author 高杰
	 * @time 2018年12月27日 上午11:45:57
	 * @param code
	 * @return
	 * @throws Throwable
	 */
	public List<CraftsmanJobDetailTO> selectByCraftsmanId(long craftsmanId) throws Throwable {
		CraftsmanJobDetailQO craftsmanJobDetailQuery = new CraftsmanJobDetailQO().setCraftsmanId(craftsmanId);
		return dao.select(NAMESPACE, craftsmanJobDetailQuery);
	}

	/**
	 * 查询
	 * 
	 * @author 高杰
	 * @time 2018年12月27日 上午11:45:57
	 * @param code
	 * @return
	 * @throws Throwable
	 */
	public CraftsmanJobDetailTO selectByCraftsmanIdAndTime(long craftsmanId, Date startTime, Date endTime)
			throws Throwable {
		CraftsmanJobDetailQO craftsmanJobDetailQuery = new CraftsmanJobDetailQO().setCraftsmanId(craftsmanId)
				.setEndTime(endTime).setStartTime(startTime);
		List<CraftsmanJobDetailTO> list = dao.select(NAMESPACE, craftsmanJobDetailQuery);
		if (list == null || list.size() < 1)
			return null;
		return list.get(0);
	}

	/**
	 * 添加
	 * 
	 * @author 高杰
	 * @time 2018年12月27日 上午11:46:03
	 * @param craftsmanJob
	 * @return
	 * @throws Throwable
	 */
	public void insert(CraftsmanJobDetailTO craftsmanJobDetail) throws Throwable {
		craftsmanJobDetail.initId(IdentityUtil.nextId(Worker.USER));
		if (dao.insert(NAMESPACE, craftsmanJobDetail) < 1)
			throw new DataException(DataException.Type.FAIL, "一线员工工作经历信息");
	}

	/**
	 * 添加
	 * 
	 * @author 高杰
	 * @time 2018年12月27日 下午2:22:31
	 * @param list
	 * @return
	 * @throws Throwable
	 */
	public void insert(List<CraftsmanJobDetailTO> list) throws Throwable {
		if (dao.insert(NAMESPACE, list) < list.size())
			throw new DataException(DataException.Type.FAIL, "一线员工工作经历信息");
	}

	/**
	 * 修改
	 * 
	 * @author 高杰
	 * @time 2018年12月27日 上午11:46:08
	 * @param craftsmanJob
	 * @return
	 * @throws Throwable
	 */
	public void update(CraftsmanJobDetailTO craftsmanJobDetail) throws Throwable {
		if (dao.update(NAMESPACE, craftsmanJobDetail) < 1)
			throw new DataException(DataException.Type.FAIL, "一线员工工作经历信息");
	}

	/**
	 * 修改
	 * 
	 * @author 高杰
	 * @time 2018年12月27日 上午11:46:08
	 * @param craftsmanJob
	 * @return
	 * @throws Throwable
	 */
	public int update(List<CraftsmanJobDetailTO> craftsmanJobDetailList) throws Throwable {
		int count = 0;
		for (Iterator<CraftsmanJobDetailTO> iterator = craftsmanJobDetailList.iterator(); iterator.hasNext();) {
			count += dao.update(NAMESPACE, iterator.next());
		}
		return count;
	}
}
